﻿using System;
using System.Data;
using System.Windows.Forms;
using HRM.HRMServiceProxy;
using WeifenLuo.WinFormsUI.Docking;
using System.Collections.Generic;
using System.Drawing;

namespace HRM
{
    public partial class frmNghiThaiSanList : FormBase
    {
        int pageSize = UIParamater.pageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        int department_ID = 0;
        int branch_ID = Program.intBranchID;

        bool cboMonthFirstLoad = true;
        MainServiceClient wcfMain;

        EmployeeClient wcfEmployee;
        RoleRightInfo listRole;

        DataTable dtNhanVien = null;

        public override void Show_Form()
        {
            this.Show();
        }

        public frmNghiThaiSanList()
        {
            InitializeComponent();
            LoadLanguage();

            wcfEmployee = Service.Employee();
            wcfMain = Service.MainService();
            cboMonthFirstLoad = false;

            listRole = UIParamater.Quyen_CanhBaoSapSinhNhat;

            if (!listRole.Update)
            {
                this.btnViewEmloyee.Image = global::HRM.Properties.Resources.app;
                this.btnViewEmloyee.Tag = "xem";
            }
            UISetting.LoadTreeView3(ref tvBranch);
            this.KeyPreview = true;

        }

        protected override void Init_Control()
        {
            Init_Grid();
            splitContainer1.Panel1Collapsed = true;
            //
            FormGlobals.Control_SetDefault(toolStrip1, toolStripButton1_Click);
            //
            LoadData(0);
        }

        //Khởi tạo danh sách cho grid
        DataGridHandle m_GridHandle;
        protected void Init_Grid()
        {
            List<DataGridViewColumn> arrColumn = new List<DataGridViewColumn>();
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("STT", "RowNumber", 50));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Số sổ BHXH", "Detail_InsBookNo", 110));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Họ và tên", "Employee_FullName", 180));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Ngày sinh", "Employee_DateOfBirth", 70, Globals.CS_DISPLAY_DATE_FORMAT));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("PA", "Detail_InsTypeCode", 50));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Từ tháng", "Detail_FromDate", 70, Globals.CS_DISPLAY_MONTHYEAR_FORMAT));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Đến tháng", "Detail_ToDate", 70, Globals.CS_DISPLAY_MONTHYEAR_FORMAT));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Số tháng", "Detail_TotalMonth", 80));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Kỳ phát sinh", "Document_MonthYear", 100));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Mã chính", "Employee_ID", 0));
            //
            m_GridHandle = FormGlobals.Grid_Init(dgvList, arrColumn, "", 1);
            m_GridHandle.Cell_Format += new DataGridHandle.Cell_FormatEventHandler(m_GridHandle_Cell_Format);
        }

        void m_GridHandle_Cell_Format(string columnName, DataGridViewCellFormattingEventArgs e)
        {
            switch (columnName.ToUpper())
            {
                case "DETAIL_TOTALMONTH":
                    if (Globals.Object_GetValue<int>(e.Value) > 6)
                    {
                        dgvList.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Yellow;
                        dgvList.Rows[e.RowIndex].DefaultCellStyle.SelectionBackColor = Color.WhiteSmoke;
                    }
                    break;
            }
        }

        void LoadLanguage()
        {
            if (this.Tag != null)
                this.TabText = this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
        }

        //private void BuildVoucherGrid()
        //{
        //    UISetting.AddColumn(ref dgvList, "RowNumber", "STT", "C", 50, true, true);
        //    UISetting.AddColumn(ref dgvList, "Employee_ID", "Mã chính", "", 1, false, true);
        //    UISetting.AddColumn(ref dgvList, "Employee_FullName", UpdateUI.GetString("hovaten"), "", 200, true, true);
        //    UISetting.AddColumn(ref dgvList, "Employee_DateOfBirth", UpdateUI.GetString("ngaysinh"), "", 110, true, true);
        //    UISetting.AddColumn(ref dgvList, "Detail_InsBookNo", UpdateUI.GetString("sosobh"), "C", 110, true, true);
        //    UISetting.AddColumn(ref dgvList, "Detail_InsTypeCode", UpdateUI.GetString("phuongan"), "C", 110, true, true);
        //    UISetting.AddColumn(ref dgvList, "Detail_FromDate", UpdateUI.GetString("tuthang"), "C", 110, true, true);
        //    UISetting.AddColumn(ref dgvList, "Detail_ToDate", UpdateUI.GetString("denthang"), "C", 110, true, true);
        //    UISetting.AddColumn(ref dgvList, "Detail_TotalMonth", "Số tháng", "C", 110, true, true);
        //    UISetting.AddColumn(ref dgvList, "Document_MonthYear", "Kỳ phát sinh", "C", 110, true, true);
        //}

        private void LoadData(object iPageIdx)
        {
            currPage = Globals.Object_GetValue<int>(iPageIdx);
            currPage = currPage >= 0 ? currPage : 0;
            //
            DataSet _ds = wcfEmployee.Employee_NghiThaiSanList(out itemCount, branch_ID, department_ID, currPage, pageSize);

            if (_ds != null)
            {
                dtNhanVien = _ds.Tables[0];
                if (!dtNhanVien.Columns.Contains("Document_MonthYear"))
                {
                    dtNhanVien.Columns.Add("Document_MonthYear");
                    dtNhanVien.Columns["Document_MonthYear"].Expression = "Document_Month + '/' + Document_Year";
                }
            }
            else if (dtNhanVien != null)
                dtNhanVien.Clear();
            dgvList.DataSource = dtNhanVien;
            //
            dtNhanVien.DefaultView.RowFilter = GetRowFilter();
            pager.PageIndex = currPage;

            FormGlobals.Control_SetViewOnly(dgvList, true);
            //pager.TotalRow = ;
           

            //dgvList.Rows.Clear();
            //for (int i = 0; i < _ds.Tables[0].Rows.Count; i++)
            //{
            //    dgvList.Rows.Insert(i, 1);
            //    dgvList.Rows[i].Cells["RowNumber"].Value = _ds.Tables[0].Rows[i]["RowNumber"].ToString();
            //    dgvList.Rows[i].Cells["Employee_ID"].Value = _ds.Tables[0].Rows[i]["Employee_ID"].ToString();
            //    //dgvList.Rows[i].Cells["Employee_Code"].Value = _ds.Tables[0].Rows[i]["Employee_Code"].ToString();
            //    dgvList.Rows[i].Cells["Employee_FullName"].Value = _ds.Tables[0].Rows[i]["Employee_LastName"].ToString()
            //        + " " + _ds.Tables[0].Rows[i]["Employee_FirstName"].ToString();
            //    dgvList.Rows[i].Cells["Employee_DateOfBirth"].Value = Convert.ToDateTime(_ds.Tables[0].Rows[i]["Employee_DateOfBirth"]).ToString("dd/MM/yyyy");
            //    dgvList.Rows[i].Cells["Detail_InsBookNo"].Value = _ds.Tables[0].Rows[i]["Detail_InsBookNo"].ToString();
            //    dgvList.Rows[i].Cells["Detail_InsTypeCode"].Value = _ds.Tables[0].Rows[i]["Detail_InsTypeCode"].ToString();
            //    dgvList.Rows[i].Cells["Detail_FromDate"].Value = Convert.ToDateTime(_ds.Tables[0].Rows[i]["Detail_FromDate"]).ToString("MM/yyyy");
            //    dgvList.Rows[i].Cells["Detail_ToDate"].Value = Convert.ToDateTime(_ds.Tables[0].Rows[i]["Detail_ToDate"]).ToString("MM/yyyy");
            //    dgvList.Rows[i].Cells["Document_MonthYear"].Value = _ds.Tables[0].Rows[i]["Document_Month"].ToString() + "/" + _ds.Tables[0].Rows[i]["Document_Year"].ToString();

            //    dgvList.Rows[i].Cells["Detail_TotalMonth"].Value = _ds.Tables[0].Rows[i]["Detail_TotalMonth"].ToString();
            //    //dgvList.Rows[i].Cells["Detail_TotalMonth"].Visible = false;
            //    //Đánh dấu thời gian nghỉ quá hạn
            //    //if (Convert.ToDateTime(_ds.Tables[0].Rows[i]["Detail_ToDate"]) <= DateTime.Now){
            //    if (Convert.ToInt32(dgvList.Rows[i].Cells["Detail_TotalMonth"].Value) > 6)
            //    {
            //        dgvList.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
            //        dgvList.Rows[i].DefaultCellStyle.SelectionBackColor = Color.Yellow;
            //    }
            //}
            //
        }

        private string GetRowFilter()
        {
            string sFilter = "";

            if(!String.IsNullOrEmpty(txtTuKhoa.Text))
            {
                sFilter = "Detail_InsBookNo like '%" + txtTuKhoa.Text + "%' OR Employee_FullName LIKE '%" + txtTuKhoa.Text + "%'";
            }
            return sFilter;
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            //string _month = cboMonth.Text;
            //string _ReportText = "sinhnhattrongthang";
            //frmPreviewReport f = new frmPreviewReport("Birth day in month", new string[] { _ReportText, _month, branch_ID.ToString(), department_ID.ToString() });
            //f.Show(this);
        }

        private void btnViewEmloyee_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedCells.Count > 0)
            {
                int Employee_ID = Convert.ToInt32(dgvList.Rows[dgvList.SelectedCells[0].RowIndex].Cells["Employee_ID"].Value);
                Form f = new frmEmployeeInfo(Employee_ID, 0, 0, UIParamater.Employee_Active, listRole);
                if (f.ShowDialog(this) == DialogResult.OK)
                {
                    LoadData(currPage);
                }
            }
        }

        private void tvBranch_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Level == 0)
            {
                if (Program.isAdmin || Program.isViewAll)
                {
                    branch_ID = 0;
                    department_ID = 0;
                }
                else
                    branch_ID = Program.intBranchID;
                LoadData(currPage);
            }
            else if (e.Node.Level == 1)
            {
                branch_ID = Convert.ToInt32(e.Node.Tag);
                department_ID = 0;
                currPage = 0;
                LoadData(currPage);
            }
            else if (e.Node.Level >= 2)
            {
                department_ID = Convert.ToInt32(e.Node.Tag);
                branch_ID = Convert.ToInt32(e.Node.Parent.Tag);
                currPage = 0;
                LoadData(currPage);

            }
        }


        private void cboMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            currPage = 0;
            if(!cboMonthFirstLoad)
                LoadData(currPage);
        }

        private void frmBirthDayList_Activated(object sender, EventArgs e)
        {
            LoadLanguage();
        }

        private void frmBirthDayList_FormClosing(object sender, FormClosingEventArgs e)
        {
            wcfEmployee.Close();
            wcfMain.Close();
        }

        private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            btnViewEmloyee_Click(null, null);

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            FormGlobals.App_ProgressShow(delegate() { 
            if (dtNhanVien != null)
                dtNhanVien.DefaultView.RowFilter = GetRowFilter();
            //LoadData(currPage);
            }, "Loading");
        }


        private void pager_PageIndex_Changed(int pageIndex)
        {
            try
            {
                FormGlobals.App_ProgressShow(new SubInvoker(LoadData), pageIndex, "");
            }
            catch (Exception ex)
            {
                FormGlobals.Message_Error(ex);
            }
        }
    }
}
